import Vue from "vue";
import VueRouter from "vue-router";
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Table from '../components/Table.vue'
import Wailian from '../components/Wailian.vue'
import Zhuye from '../components/Zhuye.vue'
import Luyou from '../components/Luyou.vue'
Vue.use(VueRouter)
const router = new VueRouter({
    routes: [
        {
            path: '/home',
            name: 'Home',
            component: Home,
            redirect:"/home/table",
            children:[
                {
                    path:"table",
                    name:"Table",
                    component:Table
                },
                {
                    path:"wailian",
                    name:"Wailian",
                    component:Wailian
                },
                {
                    path: '/zhuye',
                    name: 'Zhuye',
                    component: Zhuye,
                },
                {
                    path: '/luyou',
                    name: 'Luyou',
                    component: Luyou,
                }
            ]
        },
        {
            path: '/login',
            name: 'Login',
            component: Login
        },
        
        
    ]
})

router.beforeEach((to, from, next) => { 
    // 权限认证
    let token = localStorage.getItem('token');
    if (to.path === '/login') {
        next();
    } else {
        // 非登录页，需要权限
        if (token) {//有权限直接下一步
            next()
        } else {//没有权限去登录获得权限
            next({ name: "Login" })
        }
    }
})

export default router;